//
//  LayoutItem.m
//  Menu
//
//  Created by trimad on 11-11-11.
//  Copyright 2011 Trimad Group. All rights reserved.
//

#import "LayoutItemView.h"
#import "Item.h"
#import "OrderItem.h"
#import "OrderManager.h"
#import "MenuViewController.h"
#import "ItemDetailViewController.h"
#import "ContextManager.h"
#import "OrderManager.h"


@implementation LayoutItemView

@synthesize codeLabel;
@synthesize nameLabel;
@synthesize priceLabel;
@synthesize quantityLabel;
@synthesize unitLabel;
@synthesize descriptionLabel;
@synthesize imageView;
@synthesize removeOneButton;
@synthesize addOneButton;
@synthesize orderItem;

static int ORDER_SUMMARY_BUTTON_CENTER_X = 610;
static int ORDER_SUMMARY_BUTTON_CENTER_Y = 50;

- (void) activate: (Item *) item {
	orderItem = [[OrderItem alloc] init];
	if ([[OrderManager getInstance] getOrderItem:item.code] != nil) {
		orderItem = [[OrderManager getInstance] getOrderItem:item.code];
	} else {
		orderItem.item = item;
		orderItem.quantity = 0;
	}
	
	codeLabel.text = item.code;
	nameLabel.text = item.name;
	priceLabel.text = [[NSString alloc] initWithFormat:@"%.2f (元)", item.price];
	quantityLabel.text = [[NSString alloc] initWithFormat:@"%i", orderItem.quantity];
	unitLabel.text = orderItem.item.unit;
	descriptionLabel.text = item.name;
	
	// remove one button
	[removeOneButton addTarget:self 
						action:@selector(removeOneClicked:) 
			  forControlEvents:UIControlEventTouchUpInside];
	
	// add one button
	[addOneButton addTarget:self 
					 action:@selector(addOneClicked:) 
		   forControlEvents:UIControlEventTouchUpInside];
	
	// mid image
	imageView.image = [UIImage imageNamed:item.midImageName];
	
	UITapGestureRecognizer *tapToOpenGesture = 
	[[UITapGestureRecognizer alloc] initWithTarget:self 
											action:@selector(handleTapGestures:)];
	tapToOpenGesture.numberOfTapsRequired = 1;
	[imageView addGestureRecognizer:tapToOpenGesture];
	[[OrderManager getInstance] addDelegate:(OrderManagerDelegate *)self];
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code.
 }
 */

- (void)dealloc {
	[codeLabel release];
	[nameLabel release];
	[priceLabel release];
	[quantityLabel release];
	[addOneButton release];
	[removeOneButton release];
	[imageView release];
	[orderItem release];
    [super dealloc];
}

-(void) addOneClicked: (id) sender {
	orderItem.quantity++;
	quantityLabel.text = [[NSString alloc] initWithFormat:@"%i", orderItem.quantity];
	[removeOneButton setImage:[UIImage imageNamed:@"icon_-1_4.png"] forState:UIControlStateNormal];
	[UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveLinear 
					 animations:^{
						 quantityLabel.transform = CGAffineTransformRotate(quantityLabel.transform, M_PI);
					 }
					 completion:^(BOOL finish){
						 quantityLabel.transform = CGAffineTransformRotate(quantityLabel.transform, M_PI);
					 }
     ];
	// image animation
	UIImageView *animImageView = [[UIImageView alloc] initWithImage:imageView.image];
	animImageView.frame = imageView.frame;
	[self addSubview:animImageView];
	animImageView.alpha = 0.4f;
	[UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveLinear 
					 animations:^{
						 CGPoint point;
						 point.x = ORDER_SUMMARY_BUTTON_CENTER_X;
						 point.y = ORDER_SUMMARY_BUTTON_CENTER_Y;
						 
						 CGRect endFrame;
						 endFrame.origin = [self convertPoint:point 
												fromView:[ContextManager getInstance].rootViewController.view];
						 endFrame.size.height = 0;
						 endFrame.size.width = 0;
						 animImageView.frame = endFrame;
					 }
					 completion:nil
     ];
	[animImageView release];
	
	[[OrderManager getInstance] updateOrderItem:orderItem];
}

- (void) removeOneClicked: (id) sender {
	if (orderItem.quantity > 0) {
		orderItem.quantity--;
		quantityLabel.text = [[NSString alloc] initWithFormat:@"%i", orderItem.quantity];
		[UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveLinear 
						 animations:^{
							 quantityLabel.transform = CGAffineTransformRotate(quantityLabel.transform, M_PI);
						 }
						 completion:^(BOOL finish){
							 quantityLabel.transform = CGAffineTransformRotate(quantityLabel.transform, M_PI);
						 }
		 ];
		[[OrderManager getInstance] updateOrderItem:orderItem];
	} 
	if (orderItem.quantity == 0) {
		[removeOneButton setImage:[UIImage imageNamed:@"icon_-1_5.png"] forState:UIControlStateNormal];
	}
}

-(void) handleTapGestures:(UIGestureRecognizer *) sender {
	MenuViewController *menuVC = [[ContextManager getInstance] rootViewController];
	ItemDetailViewController *itemDetailVC = [[ItemDetailViewController alloc] 
											  initWithNibName:@"ItemDetailViewController" 
											  bundle:nil 
											  pageIndex:orderItem.item];
	itemDetailVC.view.alpha = 0.7f;
	[menuVC.view addSubview:itemDetailVC.view];
	[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionCurveLinear 
					 animations:^{
						 itemDetailVC.view.alpha = 1.0f;
					 }
					 completion:nil
	 ];
}

-(void)didUpdateOrderItem:(OrderItem *)aOrderItem {
	if ([orderItem.item.code isEqualToString:aOrderItem.item.code]) {
		orderItem = aOrderItem;
		quantityLabel.text = [[NSString alloc] initWithFormat:@"%i", orderItem.quantity];
	}
}
-(void)didRemoveOrderItem:(OrderItem *)aOrderItem {
	if ([orderItem.item.code isEqualToString:aOrderItem.item.code]) {
		orderItem.quantity = 0;
		quantityLabel.text = [[NSString alloc] initWithFormat:@"%i", orderItem.quantity];
	}
}

@end
